﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KoshiDE;

namespace Solvers.Koshi
{
    public class NonStaticAdamsSolver : KoshiSolver
    {
        public NonStaticAdamsSolver(KoshiTask task)
            : base(task)
        {
            this.Solved = false;
        }

        public override void Solve()
        {
            double F1, F2, F3, F4;
            double h = this.Task.H;
            int i = 0;
            List<double> x = new List<double>();
            List<double> y = new List<double>();
            x.Add(this.Task.A);
            y.Add(this.Task.Y0);

            KoshiSolution sol = new KoshiSolution();

            for (double Xo = this.Task.A; Xo < this.Task.B; Xo += h, i++)
            {
                x.Add(x[i] + h);

                F1 = this.Task.funct(x[i], y[i]);
                F2 = this.Task.funct(x[i] + h / 2.0, y[i] + h / 2.0 * F1);
                F3 = this.Task.funct(x[i] + h / 2.0, y[i] + h / 2.0 * F2);
                F4 = this.Task.funct(x[i + 1], y[i] + h * F3);
                y.Add(y[i] + (F1 + 2 * F2 + 2 * F3 + F4) * h / 6.0);
            }

            for (i = 3; i < (this.Task.B - this.Task.A) / h; i++)
            {
                y[i + 1] = (1.0 / 6.0) * h * ((2.5) * this.Task.funct(x[i + 1], y[i + 1]) + 4 * this.Task.funct(x[i], y[i]) - 0.5 * this.Task.funct(x[i - 1], y[i - 1])) + y[i];
                sol[x[i]] = y[i + 1];
            }

            this.Solution = sol;
            this.Solved = true;
        }
    }
}
